package 力扣日常刷题.木23四月.第4天404;

public class Demo30编辑距离 {


    public int minDistance(String world1,String world2) {
        int n = world1.length(),m = world2.length();

        int[][] dp = new int[n+1][m+1];
        for (int i = 0; i < n+1; i++) {
            dp[i][0] = i;
        }
        for (int i = 0; i < m+1; i++) {
            dp[0][i] = i;
        }

        for (int i = 1; i < n+1; i++) {
            for (int j = 1; j < m + 1; j++) {
                int l = dp[i - 1][j] + 1;
                int d = dp[i][j - 1] + 1;
                int ld = dp[i - 1][j - 1];
                if(world1.charAt(i - 1) != world2.charAt(j - 1))ld++;
                dp[i][j] = Math.min(l,Math.min(d,ld));
            }
        }
        return dp[n][m];


    }
}
